package com.tanjie.blackbean.system.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.tanjie.blackbean.mybatis.query.Condition;
import com.tanjie.blackbean.mybatis.query.PageParam;
import com.tanjie.blackbean.mybatis.support.PageConverter;
import com.tanjie.blackbean.system.admin.dao.PermissionDaoService;
import com.tanjie.blackbean.system.admin.dao.PermissionMapper;
import com.tanjie.blackbean.system.admin.dao.RolePermissionMapper;
import com.tanjie.blackbean.system.admin.model.permission.PermissionDTO;
import com.tanjie.blackbean.system.admin.model.permission.PermissionPO;
import com.tanjie.blackbean.system.admin.model.permission.PermissionQuery;
import com.tanjie.blackbean.system.admin.model.permission.PermissionVO;
import com.tanjie.blackbean.system.admin.model.rel.RolePermissionPO;
import com.tanjie.blackbean.system.admin.service.IPermissionService;
import com.tanjie.blackbean.pojo.exception.BizException;
import com.tanjie.blackbean.pojo.response.BizErrorCode;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 权限表
 *
 * @author tanjie
 * @since 2022-04-21
 */
@Service
public class PermissionServiceImpl implements IPermissionService {

    @Resource
    PermissionMapper permissionMapper;

    @Resource
    PermissionDaoService permissionService;

    @Resource
    RolePermissionMapper rolePermissionMapper;

    @Override
    public void create(PermissionDTO dto) {
        PermissionPO createOne = BeanUtil.copyProperties(dto, PermissionPO.class);
        permissionMapper.insert(createOne);
    }

    @Override
    public void update(PermissionDTO dto) {
        String id = dto.getId();
        if (StrUtil.isBlank(id)) {
            throw new BizException(BizErrorCode.ID_REQUIRE_NOT_BLANK);
        }
        PermissionPO updateOne = BeanUtil.copyProperties(dto, PermissionPO.class);
        permissionMapper.updateById(updateOne);
    }

    @Override
    public void delete(String id) {
        if (StrUtil.isBlank(id)) {
            throw new BizException(BizErrorCode.ID_REQUIRE_NOT_BLANK);
        }
        PermissionPO existOne = permissionMapper.selectById(id);
        if (existOne == null) {
            throw new BizException(BizErrorCode.ID_NOT_EXIST);
        }
        permissionMapper.deleteById(id);
    }

    @Override
    public PermissionVO detail(String id) {
        if (StrUtil.isBlank(id)) {
            throw new BizException(BizErrorCode.ID_REQUIRE_NOT_BLANK);
        }
        PermissionPO existOne = permissionMapper.selectById(id);
        if (existOne == null) {
            throw new BizException(BizErrorCode.ID_NOT_EXIST);
        }
        return BeanUtil.copyProperties(existOne, PermissionVO.class);
    }

    @Override
    public List<PermissionVO> queryList(PermissionQuery dto) {
        LambdaQueryWrapper<PermissionPO> queryWrapper = getQueryWrapper(dto);
        List<PermissionPO> list = permissionMapper.selectList(queryWrapper);
        return BeanUtil.copyToList(list, PermissionVO.class);
    }

    @Override
    public IPage<PermissionVO> queryPage(PermissionQuery dto, PageParam pageParam) {
        IPage<PermissionPO> page = Condition.getPage(pageParam);
        LambdaQueryWrapper<PermissionPO> queryWrapper = getQueryWrapper(dto);
        return PageConverter.convert(permissionMapper.selectPage(page, queryWrapper),
                permission -> BeanUtil.copyProperties(permission, PermissionVO.class));
    }

    @Override
    public List<String> listByRoleId(String roleId) {
        return new LambdaQueryChainWrapper<>(rolePermissionMapper)
                .eq(RolePermissionPO::getRoleId, roleId)
                .list()
                .stream()
                .map(RolePermissionPO::getPermissionId)
                .collect(Collectors.toList());
    }

    // 获取菜单绑定的所有权限节点
    @Override
    public List<PermissionPO> listByMenuId(String menuId) {
        return new LambdaQueryChainWrapper<>(permissionMapper)
                .eq(PermissionPO::getMenuId, menuId)
                .orderBy(true, false, PermissionPO::getUpdateTime)
                .list();
    }

    private LambdaQueryWrapper<PermissionPO> getQueryWrapper(PermissionQuery dto) {
        String name = dto.getName();
        return new LambdaQueryWrapper<PermissionPO>()
                .eq(PermissionPO::getMenuId, dto.getMenuId())
                .eq(StrUtil.isNotBlank(name), PermissionPO::getName, name);
    }

}
